//
//  StatusCell.swift
//  Weobo
//
//  Created by 史亚洲 on 16/1/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    
    //定义底部视图的顶部约束的属性
    var bottomViewTopConstraints: Constraint?
    var status: Status? {
        
        didSet {
            //绑定数据
            //1.设置原创微博的数据模型
            statusOriginalView.status=status
            
            //1.1卸载约束
            self.bottomViewTopConstraints?.uninstall()
            if let retween=status?.retweeted_status {
                //转发微博
                
                //1.设置数据源
                statusCellRetweetedView.retweeted_status=retween
                
                //2.显示转发微博视图
                statusCellRetweetedView.hidden=false
                
                //3.更新约束,相对于微博底部+10
                statusCellBottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraints=make.top.equalTo(statusCellRetweetedView.snp_bottom).constraint
                    
                })
                
            } else {
                //1.隐藏微博转发视图
                statusCellRetweetedView.hidden=true
                
                //2.更新约束,相对原创微博视图的底部+10
                statusCellBottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraints=make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })

            }
    
        }

    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //设置选中的背景颜色
        selectionStyle = .None
        setupUI()

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    private func setupUI() {
        //backgroundColor = UIColor.yellowColor()
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusCellRetweetedView)
        contentView.addSubview(statusCellBottomView)
        
        //设置约束
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
            
        }
        
        
        //需要更改底部的toolBar约束
        statusCellRetweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.left.equalTo(statusOriginalView.snp_left)
            make.right.equalTo(statusOriginalView.snp_right)
            
        }
        //底部视图
        statusCellBottomView.snp_makeConstraints { (make) -> Void in
                self.bottomViewTopConstraints=make.top.equalTo(statusCellRetweetedView.snp_bottom).constraint
                make.right.left.equalTo(contentView)
                make.height.equalTo(35)
        }
        
        
        //给contenView设置约束条件
        contentView.snp_makeConstraints { (make) -> Void in
                make.left.right.top.equalTo(self)
            //自动计算行高的最关键的一条约束
            make.bottom.equalTo(statusCellBottomView.snp_bottom)
            
        }
        
        
    }
    
    
    //如果从xib/sb加载,会调用这个方法
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    //自定义微博
    private lazy var statusOriginalView: StatusOriginalView = StatusOriginalView()
    
    //底部视图
    private lazy var statusCellBottomView: StatusCellBottomView = StatusCellBottomView()
    
    //转发微博
    private lazy var statusCellRetweetedView: StatusCellRetweetedView=StatusCellRetweetedView()
    
}
